#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include "DES.c"

#define MAXLINE 4096

int K_client[64] = { 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0,
                     0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0 };

int K_tgs[64] = {1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0,
                 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1 };

char K_client_tgs[65] = "1010101001010101110010100000111110100101110000111011001011101101";

int size;
char inputTexts[100][65];
int input[100][64];

////消息A 
int inputA[100][64];
char sendlineA[MAXLINE];

void intToString(int a[][64], int size, char* sendline) {
    int index = 0;
    for (int i = 0; i < size; ++i) {
        for (int j = 0; j < 64; ++j) {
            sendline[index++] = a[i][j] + 48;
        }
    }
    sendline[index] = '\0';
}



//消息B
char clientID[4] = "001";
char clientAdr[9] = "11100000";
char validity[6] = "11011";
char inputB_str[4096];
int inputB[100][64];
char sendlineB[MAXLINE];

int main(int argc, char** argv){
    int  listenfd, connfd;    
    struct sockaddr_in  servaddr;
    char  buff[4096];
    int  n;
    if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){
        printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);
        return 0;
   }

    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    servaddr.sin_port = htons(8080);

    if(bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){
        printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);
        return 0;
    }

    if(listen(listenfd, 10) == -1){
        printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno);
        return 0;
    }

    printf("======waiting for client's request======\n");
    while(1){
        //accept发生阻塞
        if((connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1){
            printf("accept socket error: %s(errno: %d)",strerror(errno),errno);
            continue;
        }
        n = recv(connfd, buff, MAXLINE, 0);
        buff[n] = '\0';
        printf("%s", buff);

        //消息A
        for (int i = 0; i < 64; ++i) {
            inputTexts[0][i] = K_client_tgs[i];
        }

        size = 1;
        manageText();

        for (int i = 0; i < size; ++i) {
            for (int j = 0; j < 64; ++j) {
                inputA[i][j] = input[i][j];
            }
        }

        int cipherA[size][64];
        for (int i = 0; i < size; ++i) {
            encrypt(inputA[i], K_client, cipherA[i]);
        }

        intToString(cipherA, size, sendlineA);

        //消息B
        strncat(inputB_str, clientID, 4);
        strncat(inputB_str, clientAdr, 9);
        strncat(inputB_str, validity, 6);
        strncat(inputB_str, K_client_tgs, 65);
        for (int i = 0; i < 64; ++i) {
            inputTexts[0][i] = inputB_str[i];
        }
        for (int i = 0; i < 16; ++i) {
            inputTexts[1][i] = inputB_str[i+64];
        }

        size = 2;
        manageText();

        for (int i = 0; i < size; ++i) {
            for (int j = 0; j < 64; ++j) {
                inputB[i][j] = input[i][j];
            }
        }

        int cipherB[size][64];
        for (int i = 0; i < size; ++i) {
            encrypt(inputB[i], K_tgs, cipherB[i]);
        }

        intToString(cipherB, size, sendlineB);

        //发送消息A和消息B
        printf("======发送消息A和消息B给client======\n");
        printf("消息A: %s\n", sendlineA);
        printf("消息B: %s\n", sendlineB);

        if(send(connfd, sendlineA, strlen(sendlineA), 0) < 0){
            printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
        }
        sleep(2);

        if(send(connfd, sendlineB, strlen(sendlineB), 0) < 0){
            printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
        }

        close(connfd);


    }

    close(listenfd);
    return 0;
}